import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 生成正确的BCrypt密码哈希
 * 
 * 使用方法：
 * 1. 在IDEA中创建一个测试类
 * 2. 复制这段代码运行
 * 3. 将生成的哈希值更新到数据库
 */
public class GenerateCorrectPassword {
    
    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        
        // 生成 admin123456 的哈希
        String password = "admin123456";
        String hash = encoder.encode(password);
        
        System.out.println("原始密码: " + password);
        System.out.println("BCrypt哈希: " + hash);
        System.out.println();
        System.out.println("SQL更新语句:");
        System.out.println("UPDATE users SET password_hash = '" + hash + "' WHERE nickname = 'admin';");
        System.out.println();
        
        // 验证哈希
        boolean matches = encoder.matches(password, hash);
        System.out.println("验证结果: " + (matches ? "✅ 密码匹配" : "❌ 密码不匹配"));
        
        // 测试一些常见的错误密码
        System.out.println();
        System.out.println("=== 测试其他密码 ===");
        testPassword(encoder, hash, "admin");
        testPassword(encoder, hash, "123456");
        testPassword(encoder, hash, "admin123456");
    }
    
    private static void testPassword(BCryptPasswordEncoder encoder, String hash, String testPassword) {
        boolean matches = encoder.matches(testPassword, hash);
        System.out.println(testPassword + ": " + (matches ? "✅" : "❌"));
    }
}

